<template>
  <el-dialog title="分配角色" :visible="showRoleDialog" @close="btnCancel">
    <!-- el-checkbox-group选中的是 当前用户所拥有的角色  需要绑定 当前用户拥有的角色-->
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in roleLise" :key="item.id" :label="item.id">
        {{ item.name }}
      </el-checkbox>
    </el-checkbox-group>
    <el-row slot="footer" type="flex" justify="center">

      <el-button type="primary" size="small" @click="assignRoles">确定</el-button>
      <el-button size="small" @click="btnCancel">取消</el-button>

    </el-row>

  </el-dialog>
</template>

<script>
import { getRoleList } from '@/api/setting'
import { getUserInfoByID } from '@/api/user'
import { assignRoles } from '@/api/employees'
export default {
  props: {
    showRoleDialog: {
      type: Boolean,
      default: false
    },
    userID: {
      type: String,
      default: null
    }
  },
  data() {
    return {
      roleLise: [],
      roleIds: [],
      id: null
    }
  },
  created() {
    this.getRoleList()
  },
  methods: {
    btnCancel() {
      this.$emit('update:showRoleDialog', false)
      this.roleIds = []
      this.roleLise = []
    },
    async getRoleList() {
      const { rows } = await getRoleList({ page: 1, pagesize: 100 })
      console.log(rows)
      this.roleLise = rows
    },
    async getUserInfoByID(id) {
      const res = await getUserInfoByID(id)
      // console.log(res)
      this.id = id
      this.roleIds = res.roleIds
    },
    async assignRoles() {
      try {
        // console.log({ id: this.id, roleIds: this.roleIds })
        await assignRoles({ id: this.id, roleIds: this.roleIds })
        this.$message.success('修改权限成功')
      } catch (error) {
        console.log(error)
      }
      this.btnCancel()
    }
  }
}
</script>

<style>

</style>
